package com.snopy.thread.cyclicbarrier;

import java.sql.Time;
import java.util.concurrent.BrokenBarrierException;
import java.util.concurrent.CyclicBarrier;
import java.util.concurrent.TimeUnit;

/**
 * @author snopy
 * @version 1.0.0
 * @classname DemoCycleBarrier
 * @description
 * @email 77912204@qq.com
 * @date 2022/11/19 14:12
 */
public class DemoCyclicBarrier {
    static CyclicBarrier cyclicBarrier = new CyclicBarrier(2,()->{
        System.out.println("task end---");
    });
    public static void main(String[] args) {

        new Thread(()->{
            try {
                System.out.println("task1---");
                cyclicBarrier.await();
            } catch (InterruptedException | BrokenBarrierException e) {
                throw new RuntimeException(e);
            }
        },"t1").start();
        new Thread(()->{
            try {

                TimeUnit.SECONDS.sleep(5);
                System.out.println("task2---");
                cyclicBarrier.await();

            } catch (InterruptedException | BrokenBarrierException e) {
                throw new RuntimeException(e);
            }
        },"t2").start();

    }
}
